package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.dto.SysField;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import com.jk.smartelderlycare.service.SysFieldServiceFeign;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description 添加描述
 * @Author hhk
 * @LastChangeDate 2024/11/18 14:37
 * @Version v1.0
 */

@Api(tags = "系统字段管理")
@Slf4j
@RestController
@RequestMapping(value = "field")
public class SysFieldController {
    @Autowired
    private SysFieldServiceFeign sysFieldServiceFeign;
    /**
     * @description: 后端分页查询
     * @param: [sysField, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysField>>
     * @author hhk
     * @date: 2024/11/18 14:43
     */
    @ApiOperation(value = "后端分页查询",httpMethod = "POST",notes ="后端分页查询" )
    @PostMapping(value = "findSysFieldPaginationList")
    public ResponseResult<PagedResponse<SysField>> findSysFieldPaginationList(@RequestBody SysField sysField, @RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200,"查询成功",sysFieldServiceFeign.findSysFieldPaginationList(sysField,pageNum,pageSize));
    }
    /**
     * @description: 添加系统字段
     * @param: [sysField]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/18 14:47
     */
    @ApiOperation(value = "新增系统字段",httpMethod = "POST",notes ="新增系统字段")
    @PostMapping(value = "addSysField")
    public ResponseResult addSysField(@RequestBody SysField sysField){
        try {
            sysFieldServiceFeign.addSysField(sysField);
            return new ResponseResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"添加失败");
        }
    }
    /**
     * @description: 根据id删除系统字段
     * @param: [fieldId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/18 14:48
     */
    @ApiOperation(value = "根据id删除系统字段",httpMethod = "DELETE",notes ="根据id删除系统字段")
    @DeleteMapping(value = "deleteSysFieldByFieldId")
    public ResponseResult deleteSysFieldByFieldId(@RequestParam(value = "fieldId") Long fieldId){
        try {
            sysFieldServiceFeign.deleteSysFieldByFieldId(fieldId);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }
    /**
     * @description: 根据id查询系统字段
     * @param: [fieldId]
     * @return: com.jk.smartelderlycare.dto.SysField
     * @author hhk
     * @date: 2024/11/18 14:50
     */
    @ApiOperation(value = "根据id查询系统字段",httpMethod = "GET",notes ="根据id查询系统字段")
    @GetMapping(value = "findSysFieldById")
    private ResponseResult<SysField> findSysFieldById(Long fieldId){
        return new ResponseResult<>(200,"查询成功",sysFieldServiceFeign.findSysFieldById(fieldId));
    }
    /**
     * @description: 修改系统字段
     * @param: [sysField]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/18 14:51
     */
    @ApiOperation(value = "修改系统字段",httpMethod = "PUT",notes ="修改系统字段")
    @PutMapping(value = "updataSysField")
    private ResponseResult updataSysField(@RequestBody SysField sysField){
        try {
            //获取当前登陆人
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            sysField.setModifyUser(loginUser.getUser().getUserName());
            sysField.setModifyTime(new Date());
            sysFieldServiceFeign.updataSysField(sysField);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    /**
     * @description: 修改状态
     * @param: [sysField]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/18 14:53
     */
    @ApiOperation(value = "修改状态",httpMethod = "PUT",notes ="修改状态")
    @PutMapping(value = "updateStatus")
    private ResponseResult updateStatus(@RequestBody SysField sysField){
        try {
            sysFieldServiceFeign.updateStatus(sysField);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }








}
